<template>
  <div class="myCollectionView">
    <div class="title">
      <div class="screen">
        <span class="screenText">筛选</span>
        <div class="filter">
          <div class="filterItem" v-for="(item, index) in filterOptions" :key="index">
            <label>
              <input type="radio" :value="item.value" name="collectionType" v-model="collectionFilter"
                @change="getFilterCollections" />
              <span>{{ item.label }}</span>
            </label>
          </div>
        </div>
      </div>
      <div class="buttons">
        <div class="button" @click="refreshCollections">
          <span class="iconfont icon-likesprograma-shoucang"></span>
          刷新收藏
        </div>
      </div>
    </div>

    <div class="content">
      <div class="collectionList" v-if="collectionList.length > 0">
        <div class="collection-item" v-for="collection in collectionList" :key="collection.collection_id">
          <div class="collection-header">
            <div class="course-info">
              <img :src="collection.course_pic" alt="课程封面" class="course-cover">
              <div class="course-details">
                <h3 class="course-name">{{ collection.course_name }}</h3>
                <p class="course-desc">{{ collection.course_description || '暂无描述' }}</p>
              </div>
            </div>
            <div class="collection-time">
              <p>收藏时间: {{ formatDate(collection.collect_time) }}</p>
              <p>课程难度: {{ getDifficultyText(collection.course_difficulty_id) }}</p>
            </div>
            <div class="collection-buttons">
              <div class="button view-btn" @click="viewCourse(collection.course_id)">
                <span class="iconfont icon-likesprograma-shangyibu1"></span>
                查看课程
              </div>
              <div class="button cancel-btn" @click="cancelCollection(collection.collection_id)">
                <span class="iconfont icon-likesprogramclear"></span>
                取消收藏
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="empty" v-else>暂&nbsp;&nbsp;无&nbsp;&nbsp;收&nbsp;&nbsp;藏&nbsp;&nbsp;内&nbsp;&nbsp;容</div>

      <!-- 分页 -->
      <div class="pagination" v-if="collectionList.length > 0">
        <div class="pagination-info">
          共 {{ total }} 条记录，当前第 {{ currentPage }}/{{ Math.ceil(total / pageSize) }} 页
        </div>
        <div class="pagination-buttons">
          <button @click="changePage(currentPage - 1)" :disabled="currentPage <= 1">上一页</button>
          <button @click="changePage(currentPage + 1)"
            :disabled="currentPage >= Math.ceil(total / pageSize)">下一页</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'myCollectionView',
}
</script>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

// 收藏数据结构
interface UserCollection {
  collection_id: number;     // 收藏ID
  user_id: number;           // 用户ID 
  course_id: number;         // 课程ID
  collect_time: string;      // 收藏时间
  course_name: string;      // 课程名称
  course_description: string; // 课程描述
  course_pic: string;       // 课程封面
  course_difficulty_id: number; // 课程难度ID
}

// 过滤选项
const filterOptions = [
  { label: '全部收藏', value: 'all' },
  { label: '课程收藏', value: 'course' },
  { label: '题目收藏', value: 'course' },
  { label: '笔记收藏', value: 'note' }
];

// 收藏过滤条件
const collectionFilter = ref('all');

// 模拟收藏数据
const allCollections = ref<UserCollection[]>([
  {
    collection_id: 1,
    user_id: 101,
    course_id: 201,
    collect_time: '2023-09-15T10:30:00',
    course_name: '前端开发进阶',
    course_description: '深入学习Vue3、React18和现代前端工程化，掌握企业级开发技能',
    course_pic: 'https://picsum.photos/id/180/600/300',
    course_difficulty_id: 3
  },
  {
    collection_id: 2,
    user_id: 101,
    course_id: 202,
    collect_time: '2023-09-10T14:45:00',
    course_name: 'React高级教程',
    course_description: '从零构建企业级React应用，深入理解React核心原理与最佳实践',
    course_pic: 'https://picsum.photos/id/160/600/300',
    course_difficulty_id: 4
  },
  {
    collection_id: 3,
    user_id: 101,
    course_id: 203,
    collect_time: '2023-08-25T09:15:00',
    course_name: 'TypeScript实战指南',
    course_description: '系统学习TypeScript，提升代码质量和开发效率',
    course_pic: 'https://picsum.photos/id/16/600/300',
    course_difficulty_id: 3
  },
  {
    collection_id: 4,
    user_id: 101,
    course_id: 204,
    collect_time: '2023-09-18T16:20:00',
    course_name: 'Node.js后端开发',
    course_description: '从入门到精通，打造高性能Node.js应用',
    course_pic: 'https://picsum.photos/id/43/600/300',
    course_difficulty_id: 2
  },
  {
    collection_id: 5,
    user_id: 101,
    course_id: 205,
    collect_time: '2023-09-05T11:40:00',
    course_name: '数据结构与算法',
    course_description: '夯实基础，轻松应对各类技术面试',
    course_pic: 'https://picsum.photos/id/26/600/300',
    course_difficulty_id: 5
  },
  {
    collection_id: 6,
    user_id: 101,
    course_id: 206,
    collect_time: '2023-08-10T13:50:00',
    course_name: 'Python数据分析',
    course_description: '掌握Python数据分析必备技能，入门数据科学领域',
    course_pic: 'https://picsum.photos/id/91/600/300',
    course_difficulty_id: 1
  }
]);

// 收藏列表 - 当前显示
const collectionList = ref<UserCollection[]>([]);

// 分页参数
const currentPage = ref(1);
const pageSize = ref(4);
const total = ref(0);

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return '未知时间';
  const date = new Date(dateStr);
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
};

// 获取难度文本
const getDifficultyText = (difficultyId: number) => {
  const difficultyMap = {
    1: '入门',
    2: '初级',
    3: '中级',
    4: '高级',
    5: '专家'
  };
  return difficultyMap[difficultyId] || '未知';
};

// 加载收藏记录
const loadCollections = (page: number = 1, size: number = 4, filter: string = 'all') => {
  let filtered: UserCollection[] = [];

  // 根据筛选条件过滤数据
  if (filter === 'all') {
    filtered = [...allCollections.value];
  } else if (filter === 'recent') {
    // 按收藏时间降序排序，取最新的
    filtered = [...allCollections.value].sort((a, b) =>
      new Date(b.collect_time).getTime() - new Date(a.collect_time).getTime()
    );
  } else if (filter === 'course') {
    // 这里实际上所有数据都是课程收藏，所以直接返回所有
    filtered = [...allCollections.value];
  }

  // 分页处理
  total.value = filtered.length;
  const start = (page - 1) * size;
  const end = Math.min(start + size, filtered.length);
  collectionList.value = filtered.slice(start, end);
};

// 根据筛选条件获取记录
const getFilterCollections = () => {
  currentPage.value = 1;
  loadCollections(currentPage.value, pageSize.value, collectionFilter.value);
};

// 刷新收藏
const refreshCollections = () => {
  loadCollections(currentPage.value, pageSize.value, collectionFilter.value);
};

// 分页变化
const changePage = (page: number) => {
  if (page < 1 || page > Math.ceil(total.value / pageSize.value)) return;
  currentPage.value = page;
  loadCollections(currentPage.value, pageSize.value, collectionFilter.value);
};

// 查看课程
const viewCourse = (courseId: number) => {
  router.push(`/course/courseDetails/${courseId}`);
};

// 取消收藏
const cancelCollection = (collectionId: number) => {
  // 模拟取消收藏操作，直接从列表中移除
  allCollections.value = allCollections.value.filter(item => item.collection_id !== collectionId);

  // 重新加载当前页数据
  loadCollections(currentPage.value, pageSize.value, collectionFilter.value);
};

// 生命周期钩子 - 组件挂载完成后加载数据
onMounted(() => {
  loadCollections();
});
</script>

<style src="@/assets/icons/iconfont.css" scoped></style>

<style scoped>
.myCollectionView {
  width: 100%;
  height: 100%;
}

.myCollectionView .title {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 筛选 */
.myCollectionView .title .screen {
  display: flex;
  justify-content: center;
  align-items: center;
}

.myCollectionView .title .screen .screenText {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  padding-right: 15px;
  color: rgba(100, 100, 100, 1);
}

.myCollectionView .title .screen .filter {
  display: flex;
  justify-content: center;
  align-items: center;
}

.myCollectionView .title .screen .filter .filterItem {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 筛选项 */
.myCollectionView .title .screen .filter .filterItem label {
  padding: 0px 15px;
  width: 100%;
  height: 35px;
  font-size: 14px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.myCollectionView .title .screen .filter .filterItem label input {
  width: 10px;
  height: 10px;
}

.myCollectionView .title .screen .filter .filterItem label input:before {
  content: "";
  display: inline-block;
  width: 100%;
  height: 100%;
  font-size: 8px;
  font-weight: bold;
  border-radius: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 1);
  outline: 2px solid rgba(180, 180, 180, 1);
}

.myCollectionView .title .screen .filter .filterItem label input:checked:before {
  content: "\2713";
  color: rgba(65, 185, 255, 1);
  outline: 2px solid rgba(65, 185, 255, 1);
}

.myCollectionView .title .screen .filter .filterItem label span {
  padding-left: 6px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 按钮 */
.myCollectionView .title .buttons {
  display: flex;
  justify-content: center;
  align-items: center;
}

.myCollectionView .title .buttons .button {
  padding: 5px 10px;
  font-size: 14px;
  display: flex;
  margin-left: 15px;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  background: rgba(77, 83, 232, 1);
  color: rgba(255, 255, 255, 1);
  cursor: pointer;
}

.myCollectionView .title .buttons .button .iconfont {
  margin-right: 5px;
  font-size: 18px;
}

.myCollectionView .title .buttons .button:hover {
  background-color: rgba(77, 83, 232, .8);
}

.myCollectionView .title .buttons .button:active {
  background-color: rgba(77, 83, 232, 1);
  color: rgba(255, 255, 255, .8);
  transition: .1s all;
}

/* 内容 */
.myCollectionView .content {
  width: 100%;
  height: calc(100% - 40px);
}

.myCollectionView .content .collectionList,
.myCollectionView .content .empty {
  width: 100%;
  margin: 10px 0px;
  height: calc(100% - 52px);
}

.myCollectionView .content .collectionList {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.collection-item {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 15px;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
}

.collection-item:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.collection-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.course-info {
  display: flex;
  align-items: center;
  gap: 15px;
  flex: 2;
}

.course-cover {
  width: 120px;
  height: 70px;
  object-fit: cover;
  border-radius: 5px;
}

.course-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
  color: #333;
}

.course-desc {
  font-size: 14px;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
}

.collection-time {
  font-size: 14px;
  color: #666;
  text-align: right;
  flex: 1;
}

.collection-buttons {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.view-btn,
.cancel-btn {
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 14px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.view-btn {
  background: rgba(77, 83, 232, 1);
  color: #fff;
}

.cancel-btn {
  background: #f56c6c;
  color: #fff;
}

.view-btn .iconfont,
.cancel-btn .iconfont {
  margin-right: 5px;
  font-size: 18px;
}

.view-btn:hover {
  background-color: rgba(77, 83, 232, .8);
}

.view-btn:active {
  background-color: rgba(77, 83, 232, 1);
  color: rgba(255, 255, 255, .8);
  transition: .1s all;
}

.cancel-btn:hover {
  background-color: rgba(245, 108, 108, .8);
}

.cancel-btn:active {
  background-color: rgba(245, 108, 108, 1);
  color: rgba(255, 255, 255, .8);
  transition: .1s all;
}

.myCollectionView .content .empty {
  font-size: 32px;
  font-weight: bold;
  font-family: "artisticFonts";
  display: flex;
  justify-content: center;
  align-items: center;
  color: rgba(180, 180, 180, 1);
}

/* 分页 */
.myCollectionView .content .pagination {
  width: 100%;
  height: 32px;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-info {
  font-size: 14px;
  color: #666;
}

.pagination-buttons {
  display: flex;
  gap: 10px;
}

.pagination-buttons button {
  padding: 5px 15px;
  border: 1px solid #dcdfe6;
  background-color: #fff;
  border-radius: 4px;
  cursor: pointer;
}

.pagination-buttons button:disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}

.pagination-buttons button:hover:not(:disabled) {
  background-color: #f2f6fc;
}

@media screen and (max-width: 768px) {
  .collection-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .course-info {
    width: 100%;
  }

  .collection-time {
    width: 100%;
    text-align: left;
  }

  .collection-buttons {
    width: 100%;
    justify-content: flex-start;
  }
}
</style>